package com.sky.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        // 准备setmeal数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 向setmeal中添加数据
        setmealMapper.insert(setmeal);
        // 准备setmeal_dish数据
        for (SetmealDish dish : setmealDTO.getSetmealDishes()) {
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(dish, setmealDish);
            // 向setmeal_dish 中添加数据
            setmealDishMapper.insertSetmealDish(setmealDish);
        }
    }
}
